﻿#include"test.h"
#include"XQMySql.hpp"
#include"XQGraphicsViewWidget.h"
#include"XQAccountData.h"
#include"XQLog.hpp"
void mysqlTest()
{
    auto mysql = new XQMySql();
    bindDeleteLater(mysql);
    //连接mysql的错误输出
    QObject::connect(mysql, &XQMySql::sqlError, [](const QString& error) {XQDebug << error; });
    mysql->connection("mysqlTest", "192.168.1.3", 3306, "Test", "NxnBGuY0JrL[HsV5", "Test");
    if (!mysql->isOpen())
    {
        XQDebug << "连接数据库失败退出测试";
        return;
    }
    XQDebug << "数据库连接成功";
    QString tableName = "User";
    XQAccountData acc;
    //向数据库中新建XQAccount表
    mysql->createTable(&acc,tableName);

    
   //初始化数据
    /*XQAccountData acc;*/
    acc.name = "xinyue";
    acc.account = "727370630";
    acc.password = "981103";
    acc.email = "727370630@qq.com";
    acc.phone = "18368998112";
    acc.portrait_data.load("新海天.jpg");

    //插入数据
    //if (!mysql->INSERT(tableName, acc.fields(), acc.toVariantList()))
    if (!mysql->INSERT(tableName,acc.toVariantMap()))
    {
        XQDebug << "插入数据失败";
        return;
    }
    XQDebug << "插入数据成功";
    
    //查询数据
    XQAccountData a;
    //auto data = mysql->findDatas(tableName, acc.fields(), "`account`=?", { acc.account });
    //if(!data.isEmpty())
    //{
    //a.setDatas(data[0]);
    if(mysql->SELECT_Where(tableName,a.fields(), & a, "`account`=?", {acc.account}))
    {
        //显示图片
        auto view = new XQGraphicsViewWidget;
        view->resize(1300,760);
        bindDeleteLater(view);
        view->setPixmap(QPixmap::fromImage(a.portrait_data));
        XQDebug << "显示图片";
        view->show();
    }
    XQAccountData newAcc;
    newAcc.account = "727370630";
    newAcc.password = "666666";
    //更新数据
    //if (!mysql->UPDATE(tableName, newAcc.fields(), newAcc.toVariantList(), "`account`=?", {acc.account}))
    if (!mysql->UPDATE(tableName, newAcc.fields(), & newAcc, "`account`=?", {acc.account}))
    {
        XQDebug << "更新数据失败";
        return;
    }
    XQDebug << "更新数据成功";
    // 
    //删除
    if (!mysql->DELETE(tableName, "`account`=?", { acc.account }))
    {
        XQDebug << "删除数据失败";
        return;
    }
    XQDebug << "删除数据成功";
}